﻿namespace KeRui.DAL
{
    using KeRui.DBUtility;
    using KeRui.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class Account
    {
        public int Add(KeRui.Model.Account model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@UserID", SqlDbType.Int, 4), new SqlParameter("@Username", SqlDbType.NVarChar, 20), new SqlParameter("@Password", SqlDbType.VarChar, 50), new SqlParameter("@CreateIP", SqlDbType.VarChar, 15), new SqlParameter("@Guid", SqlDbType.VarChar, 0x24) };
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = model.Username;
            parameters[2].Value = model.Password;
            parameters[3].Value = model.CreateIP;
            parameters[4].Value = model.Guid;
            DbHelperSQL.RunProcedure("Account_ADD", parameters, out num);
            return (int) parameters[0].Value;
        }

        public int AddNoAdmin(KeRui.Model.Account model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@UserID", SqlDbType.Int, 4), new SqlParameter("@Username", SqlDbType.NVarChar, 20), new SqlParameter("@Password", SqlDbType.VarChar, 50), new SqlParameter("@CreateIP", SqlDbType.VarChar, 15), new SqlParameter("@Guid", SqlDbType.VarChar, 0x24) };
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = model.Username;
            parameters[2].Value = model.Password;
            parameters[3].Value = model.CreateIP;
            parameters[4].Value = model.Guid;
            DbHelperSQL.RunProcedure("Account_ADDNoAdmin", parameters, out num);
            return (int) parameters[0].Value;
        }

        public int CheckLogin(int UserID, string Password)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@UserID", SqlDbType.Int, 4), new SqlParameter("@Password", SqlDbType.VarChar, 50) };
            parameters[0].Value = UserID;
            parameters[1].Value = Password;
            return DbHelperSQL.RunProcedure("Account_CheckLoginbyUserID", parameters, out num);
        }

        public int CheckLogin(string Username, string Password)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@Username", SqlDbType.NVarChar, 20), new SqlParameter("@Password", SqlDbType.VarChar, 50) };
            parameters[0].Value = Username;
            parameters[1].Value = Password;
            return DbHelperSQL.RunProcedure("Account_CheckLoginbyUsername", parameters, out num);
        }

        public void Delete(int UserID)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@UserID", SqlDbType.Int, 4) };
            parameters[0].Value = UserID;
            DbHelperSQL.RunProcedure("Account_Delete", parameters, out num);
        }

        public KeRui.Model.Account GetModel(int UserID)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@UserID", SqlDbType.Int, 4) };
            parameters[0].Value = UserID;
            KeRui.Model.Account account = new KeRui.Model.Account();
            DataSet set = DbHelperSQL.RunProcedure("Account_GetModel", parameters, "ds");
            if (set.Tables[0].Rows.Count > 0)
            {
                if (set.Tables[0].Rows[0]["UserID"].ToString() != "")
                {
                    account.UserID = int.Parse(set.Tables[0].Rows[0]["UserID"].ToString());
                }
                account.Username = set.Tables[0].Rows[0]["Username"].ToString();
                account.Password = set.Tables[0].Rows[0]["Password"].ToString();
                if (set.Tables[0].Rows[0]["CreateTime"].ToString() != "")
                {
                    account.CreateTime = DateTime.Parse(set.Tables[0].Rows[0]["CreateTime"].ToString());
                }
                account.CreateIP = set.Tables[0].Rows[0]["CreateIP"].ToString();
                return account;
            }
            return null;
        }

        public KeRui.Model.Account GetModel(string Username)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@Username", SqlDbType.NVarChar, 20) };
            parameters[0].Value = Username;
            KeRui.Model.Account account = new KeRui.Model.Account();
            DataSet set = DbHelperSQL.RunProcedure("Account_GetModelByUsername", parameters, "ds");
            if (set.Tables[0].Rows.Count > 0)
            {
                if (set.Tables[0].Rows[0]["UserID"].ToString() != "")
                {
                    account.UserID = int.Parse(set.Tables[0].Rows[0]["UserID"].ToString());
                }
                account.Username = set.Tables[0].Rows[0]["Username"].ToString();
                account.Password = set.Tables[0].Rows[0]["Password"].ToString();
                if (set.Tables[0].Rows[0]["CreateTime"].ToString() != "")
                {
                    account.CreateTime = DateTime.Parse(set.Tables[0].Rows[0]["CreateTime"].ToString());
                }
                account.CreateIP = set.Tables[0].Rows[0]["CreateIP"].ToString();
                return account;
            }
            return null;
        }

        public int Update(KeRui.Model.Account model, string oldPwd)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@UserID", SqlDbType.Int, 4), new SqlParameter("@Password", SqlDbType.VarChar, 50), new SqlParameter("@OldPassword", SqlDbType.VarChar, 50) };
            parameters[0].Value = model.UserID;
            parameters[1].Value = model.Password;
            parameters[2].Value = oldPwd;
            return DbHelperSQL.RunProcedure("Account_Update", parameters, out num);
        }
    }
}

